package com.thinkgem.jeesite.modules.gen.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.gen.entity.GenTemplate;
import com.thinkgem.jeesite.modules.gen.service.GenTemplateItemService;
import com.thinkgem.jeesite.modules.gen.service.GenTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 模板配置Controller
 *
 * @author kongxiangsheng
 * @version 2018-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/gen/genTemplate")
@Slf4j
public class GenTemplateController extends BaseController {
    @Autowired
    private GenTemplateService genTemplateService;
    @Autowired
    private GenTemplateItemService genTemplateItemService;

    @RequiresPermissions("gen:genTemplate:view")
    @RequestMapping(value = {"list", ""})
    public String list(GenTemplate genTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<GenTemplate> page = new BasePage<GenTemplate>(request, response);
        genTemplateService.page(page, new QueryWrapper<GenTemplate>(genTemplate));

        model.addAttribute("page", page);
        return "modules/gen/GenTemplate/list";
    }

    @RequiresPermissions("gen:genTemplate:view")
    @RequestMapping(value = "form")
    public String form(GenTemplate genTemplate, Model model) {
        if(StringUtils.isNotBlank(genTemplate.getId())){
            genTemplate = genTemplateService.getWithChildren(genTemplate.getId());
        }
        model.addAttribute("genTemplate", genTemplate);
        model.addAttribute("genTemplateItemList", genTemplateItemService.getAllTemplateItemDict());
        return "modules/gen/GenTemplate/form";
    }

    @RequiresPermissions("gen:genTemplate:edit")
    @RequestMapping(value = "save")
    public String save(GenTemplate genTemplate, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, genTemplate)) {
            return form(genTemplate, model);
        }
        genTemplateService.saveWithChildren(genTemplate);
        addMessage(redirectAttributes, "保存模板配置成功");
        return "redirect:" + adminPath + "/gen/genTemplate/?repage";
    }

    @RequiresPermissions("gen:genTemplate:edit")
    @RequestMapping(value = "delete")
    public String delete(GenTemplate genTemplate, RedirectAttributes redirectAttributes) {
        genTemplateService.deleteWithChildren(genTemplate);
        addMessage(redirectAttributes, "删除模板配置成功");
        return "redirect:" + adminPath + "/gen/genTemplate/?repage";
    }

}